package com.ruoyi.web.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.admin.domain.vo.request.returnGood.ReturnGoodsAddRequest;
import com.ruoyi.admin.domain.vo.request.returnGood.ReturnGoodsJoinRequest;
import com.ruoyi.admin.domain.vo.request.returnGood.ReturnGoodsPageRequest;
import com.ruoyi.admin.domain.vo.request.returnGood.ReturnGoodsUpdateRequest;
import com.ruoyi.admin.domain.vo.response.returnGood.ReturnGoodsPageResponse;
import com.ruoyi.admin.domain.vo.response.returnGood.ReturnGoodsResponse;
import com.ruoyi.admin.service.ReturnGoodsService;
import com.ruoyi.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 退货表(ReturnGoods)Controller类
 *
 * @author jianhan
 * @date 2023-01-31 09:53:25
 */
@Api(value = "后台退货表 Controller", tags = {"后台退货表"})
@RestController
@RequestMapping("/api/returnGoods")
public class ReturnGoodsController {

    @Autowired
    private ReturnGoodsService returnGoodsService;

    /**
     * 根据id获取数据
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<ReturnGoodsResponse> get(@PathVariable Long id) {
        return returnGoodsService.getId(id);
    }


    /**
     * 分页获取退货表列表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "分页获取退货表列表", notes = "分页获取退货表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", paramType = "query", dataType = "Integer")
    })
    @GetMapping
    public Result<ReturnGoodsPageResponse> pageList(@Valid ReturnGoodsJoinRequest req) {
        return returnGoodsService.page(new Page<>(req.getPageNum(), req.getPageSize()), req);
    }


    /**
     * 添加退货表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "添加退货表", notes = "添加退货表")
    @PostMapping
    public Result add(@RequestBody @Valid ReturnGoodsAddRequest req) {
        return returnGoodsService.add(req);
    }


    /**
     * 修改退货表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "修改退货表", notes = "修改退货表")
    @PutMapping()
    public Result update(@RequestBody @Valid ReturnGoodsUpdateRequest req) {
        return returnGoodsService.update(req.getReturnGoodsModel());
    }


    /**
     * 删除退货表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "删除退货表", notes = "删除退货表")
    @ApiImplicitParam(name = "ids", value = "id集合,逗号隔开", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<String> collect = Arrays.asList(ids.split(",")).stream().collect(Collectors.toList());
        Boolean result = true;
        for (String s : collect) {
            Boolean delete = returnGoodsService.delete(Long.valueOf(s));
            if (delete == false){
                result = false;
            }
        }
        return Result.judge(result);
    }

}
